package com.hzu.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzu.bean.Worker;
import com.hzu.service.impl.WorkerService;
import com.hzu.utils.JsonUtil;
import com.hzu.utils.MyBeanUtil;
import com.hzu.utils.Page;
import com.hzu.utils.QueryHelper;

public class WorkerAction extends BaseServlet {
	private static final long serialVersionUID = 2528035283657254951L;
	public Map<String, Object> data = new HashMap<String, Object>();
	public WorkerService workerService = new WorkerService();
	/**
	 * 员工列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		QueryHelper helper = new QueryHelper(Worker.class, "worker");
		Page<Worker> page = MyBeanUtil.getPage(request, Worker.class);
		page = workerService.getPageResult(helper, page);
		data.put("total", page.getTotalRecord());
		data.put("rows", page.getList());
		JsonUtil.write(response, data);
	}
	/**
	 * 添加员工
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Worker worker =MyBeanUtil.getWorker(request);
		workerService.save(worker);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", "success");
		map.put("error", "");
		JsonUtil.write(response, map);
	}

}
